﻿using Acme.BookStore.Purchase.Customergoods;
using Acme.BookStore.Purchase.NotificationOrder;
using Acme.BookStore.PurchaseDto.CustomergoodsDto;
using Acme.BookStore.PurchaseDto.NotificationOrder;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;

namespace Acme.BookStore.Purchase.NotificationOrderD
{
    public class NotificationOrderService:CrudAppService
        <NotificationOrders, NotificationOrderDto, Guid, PagedAndSortedResultRequestDto>, INotificationOrderServices
    {
        private readonly IRepository<IdentityUser, Guid> _userRepository;
        private readonly IDistributedCache<PagedResultDto<NotificationOrderDto>, string> _notificationOrderDtoCache;
        private readonly ILogger<NotificationOrderService> _logger;

        public NotificationOrderService
            (IRepository<NotificationOrders, Guid> repository,
            IRepository<IdentityUser, Guid> userRepository,
            IDistributedCache<PagedResultDto<NotificationOrderDto>, string> notificationOrderDtoCache,
            ILogger<NotificationOrderService> logger):base(repository)
        {
            _userRepository = userRepository;
            _notificationOrderDtoCache = notificationOrderDtoCache;
            _logger = logger;
        }

        public async Task<List<NotificationOrderDto>> QueryNotificationOrdersAsync(NotificationOrderQueryDto input)
        {
            var query = await Repository.GetQueryableAsync();
            if (!string.IsNullOrWhiteSpace(input.NotificationOrderNo))
                query = query.Where(x => x.NotificationOrderNo.Contains(input.NotificationOrderNo));
            if (!string.IsNullOrWhiteSpace(input.CustomerCode))
                query = query.Where(x => x.CustomerCode.Contains(input.CustomerCode));
            if (!string.IsNullOrWhiteSpace(input.CustomerName))
                query = query.Where(x => x.CustomerName.Contains(input.CustomerName));
            if (!string.IsNullOrWhiteSpace(input.NotificationStatus))
                query = query.Where(x => x.NotificationStatus.Contains(input.NotificationStatus));
            if (!string.IsNullOrWhiteSpace(input.ProductCode))
                query = query.Where(x => x.ProductCode.Contains(input.ProductCode));
            if (!string.IsNullOrWhiteSpace(input.ProductName))
                query = query.Where(x => x.ProductName.Contains(input.ProductName));
            if (!string.IsNullOrWhiteSpace(input.CustomerOrderNo))
                query = query.Where(x => x.CustomerOrderNo.Contains(input.CustomerOrderNo));
            if (input.ProductionDate.HasValue)
                query = query.Where(x => x.ProductionDate.Date == input.ProductionDate.Value.Date);

            var list = await AsyncExecuter.ToListAsync(query);
            return ObjectMapper.Map<List<NotificationOrders>, List<NotificationOrderDto>>(list);
        }
    }
}
